/****************************************************************************
*                                                                           *
* sensor.h 																    *
*                                                                           *
* ALaRI 2008												                *
*                                                                           *
****************************************************************************/


/****************************************************************************
* Includes										                            *
****************************************************************************/
#include "sensor.h"
#include <bitset>
#include <string>
#include "../../common/property.h"

CSensor::CSensor(int nFieldWidth, int nFieldHeight, int nPosX, int nPosY, 
				 QString strName) 
	: m_nFieldWidth(nFieldWidth), 
	m_nFieldHeight(nFieldHeight), 
	m_nPosX(nPosX),
	m_nPosY(nPosY),
	m_rgnbyField(NULL),
	m_strName(strName),
	m_bFieldUpdated(false),
	m_fltRefField(1.)
{
	assert(m_nFieldWidth > 0 && nFieldHeight > 0);
	m_nUniqueID = makeUniqueID(m_nPosX, nPosY);
	// allocate the data
	m_rgnbyField = new unsigned char [m_nFieldWidth*m_nFieldHeight]; 
	m_nOutputPower = CSimProperty::inst()->getOutputPower();
	m_nSensitivity = CSimProperty::inst()->getSensitivity();
	assert(NULL != m_rgnbyField);

}

CSensor::~CSensor()
{
	SafeFree(m_rgnbyField);
}

unsigned int CSensor::makeUniqueID(int nPosX, int nPosY)
{
	return ((unsigned int)((std::bitset<32>(nPosX) << 16) | 
		std::bitset<32>(nPosY)).to_ulong()); 
}

void CSensor::updateField(const unsigned char *rgnbyFieldData, int nFieldWidth, int nFieldHeight)
{
	assert(NULL != m_rgnbyField);
	assert(NULL != m_rgnbyField && NULL != rgnbyFieldData);
	assert(nFieldWidth == m_nFieldWidth && nFieldHeight == m_nFieldHeight);
	// we have to throw an exception here
	if (NULL != m_rgnbyField && NULL != rgnbyFieldData && 
		nFieldWidth == m_nFieldWidth && nFieldHeight == m_nFieldHeight)
	{
		memcpy(m_rgnbyField, rgnbyFieldData, sizeof(unsigned char)*nFieldWidth*nFieldHeight); 
		m_bFieldUpdated = true; 
	}
}

std::string CSensor::stdStringName() const 
{ 
	return (m_strName.toLocal8Bit().data() );
}

/****************************************************************************
* End of file																*
****************************************************************************/